﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IntersectionOfLines
{
    public partial class Form1 : Form
    {
        Point p = new Point();
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] s1 = PointA.Text.Split(',');
            p.Ax = double.Parse(s1[0]);
            p.Ay = double.Parse(s1[1]);
            string[] s2 = PointB.Text.Split(',');
            p.Bx = double.Parse(s2[0]);
            p.By = double.Parse(s2[1]);
            string[] s3 = PointC.Text.Split(',');
            p.Cx = double.Parse(s3[0]);
            p.Cy = double.Parse(s3[1]);
            string[] s4 = PointD.Text.Split(',');
            p.Dx = double.Parse(s4[0]);
            p.Dy = double.Parse(s4[1]);

            //直线AB 斜率 k1=(By-Ay)/(Bx-Ax)
            //直线AB方程为y=k1（x-Ax）+Ay； ==>y=k1*x-k1*Ax+Ay
            if(p.Bx - p.Ax != 0 && p.Dx-p.Cx!=0)
            {
                double k1 = (p.By - p.Ay) / (p.Bx - p.Ax);
                double b1 = p.Ay - k1 * p.Ax;
                double k2 = (p.Dy - p.Cy) / (p.Dx - p.Cx);
                double b2 = p.Cy - k1 * p.Cx;
                //k1*x+b1=k2*x+b2==>x=(b2-b1)/(k1-k2)
                if (k1 != k2)
                {
                    double x = (b2 - b1) / (k1 - k2);
                    double y = x * k1 + b1;
                    textBox5.Text = x.ToString() + ',' + y.ToString();
                }
                else
                {
                    label1.Text = "直线AB和CD平行或重合，不存在交点";
                }
            }else if (p.Bx - p.Ax == 0 && p.Dx - p.Cx != 0)
            {
                //交点x轴坐标为p.Ax
                double k1 = (p.By - p.Ay) / (p.Bx - p.Ax);
                double b1 = p.Ay - k1 * p.Ax;
                double k2 = (p.Dy - p.Cy) / (p.Dx - p.Cx);
                double b2 = p.Cy - k1 * p.Cx;
                double x = p.Ax;
                double y = k2 * x + b2;
                textBox5.Text = x.ToString() + ',' + y.ToString();

            }else if (p.Bx - p.Ax != 0 && p.Dx - p.Cx == 0)
            {
                double k1 = (p.By - p.Ay) / (p.Bx - p.Ax);
                double b1 = p.Ay - k1 * p.Ax;
                double k2 = (p.Dy - p.Cy) / (p.Dx - p.Cx);
                double b2 = p.Cy - k1 * p.Cx;
                double x = p.Dx;
                double y = k1 * x + b1;
                textBox5.Text = x.ToString() + ',' + y.ToString();


            }
            else
            {
                label1.Text = "直线AB和CD平行或重合，不存在交点";
            }
            
        }

        private void PointA_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和逗号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }
            

            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }

        private void PointB_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和分号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }
           

            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }

        private void PointC_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和分号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }
            
            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }

        private void PointD_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和分号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }
           

            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }
    }
}
